<template>
  <span class="onekit-text onekitClass 
						 this.userSelect? 'iselect': ''"
       :style="onekitStyle"
       :id="onekitId"
       ref="dom" v-html="html">
  </span>
</template>

<script>
import { STRING } from 'oneutil'
import weixin_behavior from '../../behaviors/weixin_behavior'
import onekit_behavior from '../../behaviors/onekit_behavior'
export default {
  name: "onekit-text",
  mixins: [weixin_behavior, onekit_behavior],
  data () {
    return {
      oldValue: '',
      str: ""//this.$slots.default[0].text
    }
  },
  props: {
    'user-select': {
      type: [Boolean, String],
      default: false,
      required: false,
    },
    'space': {
      type: String,
      default: null,
      required: false,
    },
    'decode': {
      type: [Boolean, String],
      default: false,
      required: false
    }
  },
  computed: {
    html () {
      let temp = this.str
      if (this.space) {
        temp = STRING.replace(temp, ' ', `&${this.space};`)
      }
      if (!this.decode) {
        temp = temp.replace(/&amp;/g, "&amp;&amp;")
        temp = temp.replace(/&lt;/g, "&amp;&lt;")
        temp = temp.replace(/&gt;/g, "&amp;&gt;")
        temp = temp.replace(/&nbsp;/g, "&amp;nbsp;")
        temp = temp.replace(/&#39;/g, "&amp;&#39;")
        temp = temp.replace(/&quot;/g, "&amp;&quot;")
        temp = temp.replace(/\\n/g, "<br/>")
        temp = temp.replace(/\n/g, "<br/>")
      }
      return temp
    }

  },
  mounted () {
    const timer = setInterval(() => {
      const str = this.$slots.default[0].text
      if(this.str!=str){
        this.str = str
      }
    }, 50)

    this.$once('hook:beforeDestroy', () => {
      clearInterval(timer)
    })
  }
}
</script>

<style>
</style>
